﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Identity.Client;
using Newtonsoft.Json;
using SenFangKaoHe_MVC.Models.Entity;
using SenFangKaoHe_MVC.Models.ViewModel;
using SenFangKaoHe_MVC.Services;
using SqlSugar;
using System.Diagnostics;

namespace SenFangKaoHe_MVC.Controllers
{
    public class ShiJuanController : Controller
    {
        private readonly SqlSugarClient db;
        public ShiJuanController(SqlSugarClient db)
        {
            this.db = db;
        }
        public IActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public async Task<IActionResult> Create(int khid, string xm)
        {
            TempShiJuan tmp_sj = new TempShiJuan();
            shijuan _oldsj = await new KaoShiServices(db).getShiJuan(khid, xm);
            if (_oldsj == null)
            {
                Debug.WriteLine("没有"+xm+"的考核信息");
                kaohe kh = await new KaoShiServices(db).getKaoHe(khid);
                var tkid = kh.TiKuid;
                int tks = tkid.Split(",").Length;
                int[] tkids = new int[tks];  //题库IDs
                for (var a = 0; a < tks; a++)
                {
                    tkids[a] = Convert.ToInt32(tkid.Split(",")[a]);
                }
                //获取题库名称
                String tkmc = await new TiKuServices(db).getTiKuMingCheng(kh.TiKuid);
                tkmc = tkmc.Replace(",", "\r\n");

                int[] txs = { 1, 2, 3, 4 };
                int[] tls = { Convert.ToInt32(kh.TianKong), Convert.ToInt32(kh.DanXuan), Convert.ToInt32(kh.DuoXuan), Convert.ToInt32(kh.PanDuan) };
                int[] fzs = { Convert.ToInt32(kh.TianKongFen), Convert.ToInt32(kh.DanXuanFen), Convert.ToInt32(kh.DuoXuanFen), Convert.ToInt32(kh.PanDuanFen) };

                List<TempShiJuanKaoTi> selectkt = await new KaoShiServices(db).getShiJuanKaoTi(tkids, txs, tls, fzs);

                tmp_sj.Id = kh.Id;
                tmp_sj.ShiTi = selectkt;
                tmp_sj.KaoHeMingCheng = kh.MingCheng;
                tmp_sj.XingMing = xm;
                tmp_sj.YiDaTiShu = 0;
                tmp_sj.KaoHeFanWei = tkmc;
                Debug.WriteLine(JsonConvert.SerializeObject(tmp_sj));
            }
            else
            {
                Debug.WriteLine("有"+xm+"的考核信息");
                tmp_sj.Id = khid;
                tmp_sj.KaoHeMingCheng = _oldsj.kaohe.MingCheng;
                tmp_sj.XingMing = xm;
                tmp_sj.ShiTi = null;
                tmp_sj.ChengJi = _oldsj.chengji;
                tmp_sj.FenZhi = _oldsj.fenzhi;
                Debug.WriteLine(JsonConvert.SerializeObject(tmp_sj));
            }
            return View(tmp_sj);
        }

        public async Task<int> SaveChengJi(int kaoheid, string xingming, string shiti_ids, string cuoti_ids, string cuoti_das, int fenzhi, int chengji)
        {
            shijuan _shijuan = new shijuan();
            _shijuan.kaoheid = kaoheid;
            _shijuan.tiliang = shiti_ids.Split(',').Length;
            _shijuan.shiti_ids = shiti_ids;
            _shijuan.cuoti_ids = cuoti_ids;
            _shijuan.cuoti_das = cuoti_das;
            _shijuan.fenzhi = fenzhi;
            _shijuan.chengji = chengji;
            _shijuan.xingming = xingming;
            Debug.WriteLine(JsonConvert.SerializeObject(_shijuan));
            int sjid = await new KaoShiServices(db).saveShiJuan(_shijuan);
            return sjid;
        }

        public async Task<IActionResult> showCuoTi(int id)
        {
            TempShiJuan sj = await new KaoShiServices(db).getCuoTis(id);
            return View(sj);
        }

        public async Task<shijuan> getShiJuan(int id)
        {
            return await new KaoShiServices(db).getShiJuan(id);
        }

        public async Task<TempShiJuan> getShiJuanCuoTi(int id)
        {
            return await new KaoShiServices(db).getCuoTis(id);
        }

    }
}
